﻿#region 命名空间

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using Sharp.Application.Implementation;
using Sharp.Domain.MainBoundedContext;
using Sharp.Domain.MainBoundedContext.MasterDataModule.Aggregates;
using Sharp.Domain.MainBoundedContext.MasterDataModule.Services;
using Sharp.Domain.MainBoundedContext.MessageModule.Aggregates;
using Sharp.Domain.MainBoundedContext.MessageModule.Services;
using Sharp.Domain.MainBoundedContext.OrgModule.Aggregates;
using Sharp.Domain.MainBoundedContext.OrgModule.Services;
using Sharp.Domain.MainBoundedContext.PermissionModule.Aggregates;
using Sharp.Domain.MainBoundedContext.PersonModule.Aggregates;
using Sharp.Domain.MainBoundedContext.PersonModule.Services;
using Sharp.Domain.MainBoundedContext.UserModule.Aggregates;
using Sharp.Domain.Repositories;
using Sharp.Infrastructure.Communication;
using Sharp.Infrastructure.InterceptionBehaviors;

using Sharp.ServiceContracts;

#endregion

namespace Sharp.Infrastructure.Crosscutting.IoC
{
    /// <summary>
    ///     DI container accesor
    /// </summary>
    public static class Container
    {

        static Container()
        {
            ConfigureContainer();

            ConfigureFactories();
        }

        public static void ConfigureContainer()
        {
            /*
             * Add here the code configuration or the call to configure the container 
             * using the application configuration file
             */
            ServiceLocator.RegisterType<IRepositoryContext, EntityFrameworkRepositoryContext>(new WcfPerRequestLifetimeManager());

            ////-> Unit of Work and repositories


            ServiceLocator.RegisterType<IUserRepository, UserRepository>();
            ServiceLocator.RegisterType<IUserGroupRepository, UserGroupRepository>();
            ServiceLocator.RegisterType<IRoleRepository, RoleRepository>();
            ServiceLocator.RegisterType<IPermissionRepository, PermissionRepository>();
            ServiceLocator.RegisterType<IUserLogRepository, UserLogRepository>();

            ServiceLocator.RegisterType<IGroupRepository, GroupRepository>();
            ServiceLocator.RegisterType<IJobRepository, JobRepository>();
            ServiceLocator.RegisterType<IOrgRepository, OrgRepository>();
            ServiceLocator.RegisterType<IOrgLevelRepository, OrgLevelRepository>();
            ServiceLocator.RegisterType<IOrgForBusinessRepository, OrgForBusinessRepository>();
            ServiceLocator.RegisterType<IOrgForBusinessListRepository, OrgForBusinessListRepository>();
            //ServiceLocator.RegisterType(typeof(IPersonRepository<>), typeof(PersonRepository<>));
            //无法编译，暂时注释掉。
            ServiceLocator.RegisterType(typeof(IEmployeeRepository), typeof(EmployeeRepository));
            ServiceLocator.RegisterType(typeof(IMasterDataRepository<>), typeof(MasterDataRepository<>));

            ServiceLocator.RegisterType<IMessageRepository, MessageRepository>();
            ServiceLocator.RegisterType<IMessageBoxRepository, MessageBoxRepository>();

            ////-> Adapters
            //ServiceLocator.RegisterType<ITypeAdapter, TypeAdapter>();
            //ServiceLocator.RegisterType<RegisterTypesMap, ERPModuleRegisterTypesMap>("erpmodule");

            ////-> Domain Service
            ServiceLocator.RegisterType(typeof(IMasterDataService<>), typeof(MasterDataService<>));
            ServiceLocator.RegisterType<IPersonService, PersonService>();
            ServiceLocator.RegisterType<Sharp.Domain.MainBoundedContext.UserModule.Services.IUserService, Sharp.Domain.MainBoundedContext.UserModule.Services.UserService>();
            ServiceLocator.RegisterType<IOrgService, OrgService>();
            ServiceLocator.RegisterType<IMessageService, MessageService>();

            //// Application Service Implementations
            ServiceLocator.RegisterType<IEnterpriseService, EnterpriseServiceImp>(
                new Interceptor<TransparentProxyInterceptor>(),
                new InterceptionBehavior<PolicyInjectionBehavior>(),
                new InterceptionBehavior(new ExceptionLoggingBehavior()));
            ServiceLocator.RegisterType<IUserService, UserServiceImpl>(
                new Interceptor<TransparentProxyInterceptor>(),
                new InterceptionBehavior<PolicyInjectionBehavior>(),
                new InterceptionBehavior(new ExceptionLoggingBehavior()));


            ServiceLocator.RegisterType(typeof(ISystemService), typeof(SystemServiceImpl),
                new Interceptor<TransparentProxyInterceptor>(),
                new InterceptionBehavior<PolicyInjectionBehavior>(),
                new InterceptionBehavior(new ExceptionLoggingBehavior())
                );

            ServiceLocator.RegisterType(typeof(IHeadData), typeof(Sharp.Platform.UserManage));

        }


        private static void ConfigureFactories()
        {
            //LogFactory.SetCurrent(new TraceSourceLogFactory());
            //EntityValidatorFactory.SetCurrent(new DataAnnotationsEntityValidatorFactory());
        }
    }
}